/* ********************************************************************************* */
/* 7 *********************** COW adoption event study ****************************** */
/* ********************************************************************************* */
/* Summary   : Code builds event study test for COW adoption and change in ROA       */
/* ********************************************************************************* */

	LIBNAME cow 'C:\cow';
	LIBNAME data 'C:\data';


* Cross-checked and fixed state of incorporation and location;
	proc import out=cow.statefix datafile="C:\data\statefix.csv" dbms=csv replace; run;



		
* merge all compustat firms using "comp" generated from Compustat.sas and header file from CRSP/Comp merged;

	data compann; set data.comp; run;
	data headercrspcomp; set data.headercrspcomp; gvkey1=gvkey*1; drop gvkey sic; run;
	data headercrspcomp; set headercrspcomp; rename gvkey1=gvkey; headerid=_N_; run;
	proc sql; create table allcomp as select * from headercrspcomp as a left join compann as b 
	on a.fyear = b.fyear and a.gvkey = b.gvkey; quit;
	proc sort data=allcomp; by headerid; run;


* get crsp annual returns, download month stock return files from CRSP;

	data msi; set data.msi; keep date VWRETD;  run;
	data msf; set data.msf; keep date cusip ret; run; proc sort; by ret; run;
	proc sql; create table msfmsi as select * from msf as a left join msi as b on a.date=b.date; quit;
	
	data temp; set headercrspcomp; keep headerid cusip datadate; run;
	proc sql; create table compmsfmsi as select * from temp as a left join msfmsi as b 
	on substr(a.cusip,1,6)=substr(b.cusip,1,6) and mdy(month(a.datadate),day(a.datadate),year(a.datadate)-1)<b.date<=a.datadate; quit;
	proc sort; by headerid date; run;

	data compmsfmsi; set compmsfmsi; AR=RET-VWRETD; l_RET=log(1+RET); l_VWRETD=log(1+VWRETD); run; proc sort; by headerid; run;
	proc means data=compmsfmsi noprint; var AR RET VWRETD; by headerid; 
	output out = mean_mth_ar (drop= _:) mean=mean_mth_ar mean_mth_ret mean_mth_vwretd; run; 
	proc means data=compmsfmsi noprint; var l_RET l_VWRETD; by headerid; 
	output out = sum_mth_ret (drop= _:) sum=s_RET s_VWRETD; run; 
	proc means data=compmsfmsi noprint; var RET; by headerid; 
	output out = std_mth_ret (drop= _:) std=std_mth_ret ; run; 
	
	data allcrsp; merge temp mean_mth_ar sum_mth_ret std_mth_ret ; by headerid; 
	bhar=exp(s_RET)-exp(s_VWRETD); drop s_: cusip datadate; run;

	data allcrspcomp; merge allcomp allcrsp; by headerid; proc sort; by headerid; run;
	data data.allcrspcomp; set allcrspcomp; by headerid; if headerid~=.; if first.headerid; run;
	proc sort data=data.allcrspcomp; by gvkey fyear; run; 



* merge with state;

	data crspcompstate; merge data.allcrspcomp(in=in1) cow.statefix ; by gvkey fyear; if in1; 
	if FIC="USA"; 
	drop LIID LINKTYPE CONSOL INDFMT DATAFMT POPSRC CURCD EXCHG FIC 
		 intexp adv ttldebt ltdebt pe OCF FCF NDI PPE EBITDA NI OCFoldassets FCFoldassets 
		 TLCF PI interestburden CFshortfall humancap GDWLIP GDWL 
		 INVT INTAN	OIBDP DVC RE WCAP DLTIS SSTK DLTR PRSTKC XINT;
	run;


	data comp; set crspcompstate;

	if mktcap>0;

	label incorp=Historical State of Incorporation ; 
	label incorpcurrent=Current State of Incorporation ; 
	label state=State of Location;

	if incorp~="" then do; 

	if (incorp in ("DE") and fyear>=2000)
	or (incorp in ("OK") and fyear>=2001)
	or (incorp in ("MO") and fyear>=2003)
	or (incorp in ("KS") and fyear>=2005)
	or (incorp in ("TX") and fyear>=2006)
	or (incorp in ("NV") and fyear>=2007)
	or (incorp in ("NJ") and fyear>=2011)
	or (incorp in ("MD") and fyear>=2014)
	or (incorp in ("WA") and fyear>=2016)
	then cow=1; else cow=0;

	end;

	state_no=stfips(state); 
	incorp_no=stfips(incorp); 
	run;



 	data b; set comp; retain yrt; *keep fyear gvkey cow yrt; l_cow=lag(cow); l_gvkey=lag(gvkey);
	if cow=1 and l_gvkey=gvkey then yrt=yrt+1; else yrt=.; if cow~=l_cow and l_gvkey=gvkey then yrt=0; drop l_cow l_gvkey; run;
	proc sort data=b; by gvkey descending fyear; run;
	data b; set b; retain yrt2; l_cow=lag(cow); l_gvkey=lag(gvkey);
	if cow=0 and l_gvkey=gvkey then yrt2=yrt2-1; else yrt2=.; if cow~=l_cow and l_gvkey=gvkey then yrt2=-1; drop l_cow l_gvkey;	run;
	proc sort data=b; by gvkey fyear; run;
	data comp; set b; if yrt=. then yrt=yrt2; drop yrt2; run;

	data all; set comp; run;
	

* lags and leads;
	proc sort data=all; by gvkey fyear; run;
	data cow; set all; run;	
	data cow_m1; set all; fyear_m1=fyear; cow_m1=cow; keep gvkey fyear_m1 cow_m1; run;
	proc sql; create table cow as select * from cow as a left join cow_m1 as b on a.fyear-1=b.fyear_m1 and a.gvkey=b.gvkey; quit; 
	data cow_m2; set all; fyear_m2=fyear; cow_m2=cow; keep gvkey fyear_m2 cow_m2; run;
	proc sql; create table cow as select * from cow as a left join cow_m2 as b on a.fyear-2=b.fyear_m2 and a.gvkey=b.gvkey; quit; 
	data cow_m3; set all; fyear_m3=fyear; cow_m3=cow; keep gvkey fyear_m3 cow_m3; run;
	proc sql; create table cow as select * from cow as a left join cow_m3 as b on a.fyear-3=b.fyear_m3 and a.gvkey=b.gvkey; quit; 
	data cow_p1; set all; fyear_p1=fyear; cow_p1=cow; keep gvkey fyear_p1 cow_p1; run;
	proc sql; create table cow as select * from cow as a left join cow_p1 as b on a.fyear+1=b.fyear_p1 and a.gvkey=b.gvkey; quit; 
	data cow_p2; set all; fyear_p2=fyear; cow_p2=cow; keep gvkey fyear_p2 cow_p2; run;
	proc sql; create table cow as select * from cow as a left join cow_p2 as b on a.fyear+2=b.fyear_p2 and a.gvkey=b.gvkey; quit; 
	data cow_p3; set all; fyear_p3=fyear; cow_p3=cow; keep gvkey fyear_p3 cow_p3; run;
	proc sql; create table cow as select * from cow as a left join cow_p3 as b on a.fyear+3=b.fyear_p3 and a.gvkey=b.gvkey; quit; 
	proc sql; drop table cow_p1, cow_p2, cow_p3, cow_m1, cow_m2, cow_m3; quit;
	proc sort data=cow; by headerid ; run; 
	data cow.cow; set cow; by headerid; if first.headerid; drop fyear_p1 fyear_p2 fyear_p3 fyear_m1 fyear_m2 fyear_m3; run;




***********************************************************;
* Event study : Keep firms in COW states and effective year;
***********************************************************;
	data event; set cow.cow;  
	fyenddt=datadate;
	if (incorp in ("DE") and year(fyenddt)=2000)
	or (incorp in ("OK") and year(fyenddt)=2001)
	or (incorp in ("MO") and year(fyenddt)=2003)
	or (incorp in ("KS") and year(fyenddt)=2005)
	or (incorp in ("TX") and year(fyenddt)=2006)
	or (incorp in ("NV") and year(fyenddt)=2007)
	or (incorp in ("NJ") and year(fyenddt)=2011)
	or (incorp in ("MD") and year(fyenddt)=2014)
	or (incorp in ("WA") and year(fyenddt)=2016);

	if incorp in ("DE") then event=mdy(6,23,2000);
	if incorp in ("OK") then event=mdy(6,4,2001);
	if incorp in ("MO") then event=mdy(7,3,2003);
	if incorp in ("KS") then event=mdy(5,17,2004);
	if incorp in ("TX") then event=mdy(5,17,2005);
	if incorp in ("NV") then event=mdy(6,13,2007);
	if incorp in ("NJ") then event=mdy(3,1,2011);
	if incorp in ("MD") then event=mdy(5,15,2014);
	if incorp in ("WA") then event=mdy(4,17,2015);
	
	format event date9.;
	permno=lpermno;	keep incorp event cusip permno gvkey fyear; run;

	proc sort data=event nodupkey; by event incorp permno; run; 
	data event; set event; eventrow=_n_; run;

* Export txt file for WRDS event study;
	data a; set event; date=event;	format date yymmddn8.;	keep permno date; run;
	PROC EXPORT DATA=a DBMS=tab	OUTFILE='C:\data\event.txt'	REPLACE;PUTNAMES=NO;RUN;

* Run Event study at WRDS Eventus to calculate CAR(-1,+1) using the market model calculated during the one-year
window ending four weeks prior to the law enactment day;
	PROC import out=eventstudy 
	FILE='C:\data\eventstudy.csv'  dbms=csv replace ; run;	

	data eventstudy; set eventstudy; if abret~=.; run;
	proc sql; create table eventreturn as select * from eventstudy as a left join event as b 
	on b.event-5<=a.date<=b.event+5 and a.permno=b.permno ; quit;

	proc sort data=eventreturn; by eventrow permno event incorp ; run;
	proc means noprint data=eventreturn; by eventrow permno event incorp; var abret ;
	output out=car(drop=_:) sum=car; run;

* use compustat SAS data file "comp" processed from "0 compustat.sas";
	data comp; set data.comp; 
	proc sort; by gvkey fyear; run;
	data comp; set comp; rdassets=rd/assets;
	lag1_gvkey=lag(gvkey); lag2_gvkey=lag2(gvkey); lag3_gvkey=lag3(gvkey); run;
	data comp; set comp; 
	if lag1_gvkey=gvkey then lag1_roa=lag(roa);
 	if lag2_gvkey=gvkey then lag2_roa=lag2(roa); 
 	if lag3_gvkey=gvkey then lag3_roa=lag3(roa); 
	if lag1_gvkey=gvkey then lag1_rdassets=lag(rdassets);
 	if lag2_gvkey=gvkey then lag2_rdassets=lag2(rdassets); 
 	if lag3_gvkey=gvkey then lag3_rdassets=lag3(rdassets); 
	run;
	proc sort; by gvkey descending fyear; run;
	data comp; set comp; lead1_gvkey=lag(gvkey); lead2_gvkey=lag2(gvkey); lead3_gvkey=lag3(gvkey); run;
	data comp; set comp; 
	if lead1_gvkey=gvkey then lead1_roa=lag(roa);
 	if lead2_gvkey=gvkey then lead2_roa=lag2(roa); 
 	if lead3_gvkey=gvkey then lead3_roa=lag3(roa); 
	run;

	proc sort data=comp; by fyear; run;
	proc means data=comp noprint ; by fyear; var kz hp ww; 
	output out=comp_med(drop=_:) q3=kz_med hp_med ww_med; run;
	data compall; merge comp comp_med; by fyear; run;


	proc sql; create table car_comp as select * from car as a left join compall as b 
	on b.fyenddt-365<=a.event<=b.fyenddt and a.permno=b.lpermno ; quit;

	
	proc sort data=car_comp; by eventrow; run;
	data car_comp; set car_comp; by eventrow; if first.eventrow; run;

	
* merge COW data and control data from main file mvc_all created from "2 mvc.sas" and "1 control variables.sas";
	proc sql; create table car_mvc_all as select * from car_comp as a left join cow.mvc_all as b 
	on b.datadate-365<=a.event<=b.datadate and a.permno=b.permno ; quit;
	proc sql; create table car_final as select * from car_mvc_all as a left join cow.controls as b 
	on b.datadate-365<=a.event<=b.datadate and a.permno=b.permno ; quit;


	PROC EXPORT DATA=car_final OUTFILE= "C:\cow\car.dta" DBMS=STATA REPLACE;RUN;




